package com.huosu.utils;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * @program: miner-shield-web
 * @description:
 * @author: changlin
 * @create: 2019-08-20 11:29
 **/
@Slf4j
public class KeyUtil {

    public static String GetMac(String brName) {
        String path = String.format("/sys/class/net/%s/address", brName);
        String mac = "";
        try {
            FileInputStream in = new FileInputStream(path);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null) {
                mac = strLine;
                mac = mac.replaceAll(":","").toUpperCase();
                break;
            }
        } catch (Exception e) {
           log.error("GetMac err {}",e);
        }
        return mac;
    }

//    public static String GetUUID(String brName) {
//        String mac = "";
//        try {
//            Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
//            while (enumeration.hasMoreElements()) {
//                StringBuffer stringBuffer = new StringBuffer();
//                NetworkInterface networkInterface = enumeration.nextElement();
//                String name = networkInterface.getDisplayName();
//                //Ethernet
//                if (networkInterface != null && brName.equals(name)) {
//                    byte[] bytes = networkInterface.getHardwareAddress();
//                    if (bytes != null) {
//                        for (int i = 0; i < bytes.length; i++) {
//                            int tmp = bytes[i] & 0xff;
//                            String str = Integer.toHexString(tmp);
//                            if (str.length() == 1) {
//                                stringBuffer.append("0" + str);
//                            } else {
//                                stringBuffer.append(str);
//                            }
//                        }
//                        mac = stringBuffer.toString().toUpperCase();
//                        break;
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return mac;
//    }
//
//    public static String GetIP(String brName) {
//        String ipA = "";
//        try {
//            Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
//            while (enumeration.hasMoreElements()) {
//                StringBuffer stringBuffer = new StringBuffer();
//                NetworkInterface networkInterface = enumeration.nextElement();
//                String name = networkInterface.getDisplayName();
//                //Ethernet
//                if (networkInterface != null && !name.contains("lo") && brName.equals(name)) {
//                    Enumeration<InetAddress> iparray = networkInterface.getInetAddresses();
//                    while (iparray.hasMoreElements()) {
//                        InetAddress ip = iparray.nextElement();
//                        ipA = ip.getHostAddress();
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return ipA;
//    }

}
